﻿//-------------------------------------------------------------------------
///-----------------_设计模式单例模式--------------------------------------
///-----------------2021/5/8---by mv-------------------------------
//-------------------------------------------------------------------------
#include "pro_mgr.h"
#include "task/task_mgr.h"
#include "common/global_include.h"
//-------------------------------------------------------------------------
mv::ProMgr* mv::ProMgr::_ProMgr = nullptr;
std::mutex pro_mx;

//-------------------------------------------------------------------------
mv::ProMgr*
mv::ProMgr::getSingleton(void)
{
    if (!_ProMgr)
    {
        std::lock_guard<std::mutex> locker(pro_mx);
        _ProMgr = new mv::ProMgr;
    }
    return _ProMgr;
}

//-------------------------------------------------------------------------
void mv::ProMgr::destroyInstance(void)
{
    if (_ProMgr)
    {
        std::lock_guard<std::mutex> locker(pro_mx);
        delete _ProMgr;
        //  if(m_pFile) delete m_pFile;
        _ProMgr = nullptr;
    }
}

//-------------------------------------------------------------------------
mv::ProMgr::ProMgr()
{
	//
}

//-------------------------------------------------------------------------
mv::ProMgr::ProMgr(const ProMgr&)
{
    //
}

void  mv::ProMgr::initPro() {

    // 初始化日志模块
    std::string unzipCmd = std::string("mkdir -p run/log ");
    system(unzipCmd.c_str());
	toolkit::Logger::Instance().add(std::make_shared<toolkit::ConsoleChannel>());
	std::string logPath = mv::ProMgr::getSingleton()->getFileMgr()->getCurPath() + "/run/log/";
	auto FileChannel = std::make_shared<toolkit::FileChannel>("FileChannel", logPath);
	FileChannel->setFileMaxCount(2);
	FileChannel->setFileMaxSize(5);
	toolkit::Logger::Instance().add(FileChannel);
	toolkit::Logger::Instance().setWriter(std::make_shared<toolkit::AsyncLogWriter>());
	InfoL << "-------------------------------------------";
    InfoL << "initPro start";
    InfoL << "当前程序的工作路径:【"<< mv::ProMgr::getSingleton()->getFileMgr()->getCurPath() <<"】";
	InfoL << "程序启动!";
	InfoL << "当前程序版本号:【" << PROJECT_VERSION << "】";
	InfoL << "-------------------------------------------";
	//启动ipc dbus消息监听
	if (!mv::ProMgr::getSingleton()->startIpcServer())
	{
		ErrorL << "start dbus client  failed！";
	}
	return;
}

//-------------------------------------------------------------------------------------
mv::FileMgr* mv::ProMgr::getFileMgr() {
    if (!m_pFile)
    {
        std::lock_guard<std::mutex> locker(pro_mx);
        m_pFile = new mv::FileMgr;
    }
    return m_pFile;
}

bool mv::ProMgr::startIpcServer(const std::string& iniParm) {
	mv::TaskMgr::Instance().initListionTask();
	// if (mv_ipc_client_init() != 0)
	// 	return false;
    return true;
}
//-------------------------------------------------------------------------
